// components/player/index.js
import { playerStore } from '../../store/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    id: 0,
    // 是否展示
    isShow: false,
    // 播放状态
    playerState: false,
    // 歌曲信息
    songData: {},
    // 歌曲列表
    songList: [],
    songIndex: 0,
    // 播放模式
    playerMode: 0,
    // 是否展示歌曲表单
    showForm: false
  },
  lifetimes: {
    attached: function() {
      // 监听store里面的数据
      this.spyData()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 监听数据
    spyData() {
      playerStore.onStates(["id", "songData", "playerState", "songList", "playerMode", "songIndex"], (
        {
          id,
          songData,
          playerState,
          songList,
          playerMode,
          songIndex
        }
        ) => {
          if(id && id != 0) {
            this.setData({isShow: true, id})
          }
          if(songData) {
            this.setData({songData})
          }
          if(playerState != this.properties.playerState) {
            this.setData({playerState})
          }
          if(songList && songList.length != 0) {
            this.setData({songList})
          }
          if(playerMode != this.data.playerMode) {
            this.setData({playerMode})
          }
          if(songIndex != undefined) {
            this.setData({songIndex})
          }
      })  
    },
    // 暂停\播放
    handleClickChangeSongState() {
      playerStore.dispatch("changePlayState")
    },
    // 点击播放栏
    handleClickPlayer() {
      wx.navigateTo({
        url: '/pages/song-player/index?id=' + this.data.id,
      })
    },
    // 展示歌曲表单
    handleClickForm() {
      this.setData({showForm: !this.data.showForm})
    }
  }
})
